package com.lsh.oms.core.exception;

/**
 * @author peter
 */
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = -1729916361850407761L;

    private String businessCode;

    private String message;

    public BusinessException(String code) {
        this.businessCode = code;
        this.message = code;
    }

    public BusinessException(String code, String message) {
        this.businessCode = code;
        this.message = message;

    }

    public BusinessException(String code, String message, Throwable cause) {
        super(cause);
        this.businessCode = code;
        this.message = message;
    }

    public String getCode() {
        return businessCode;
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder(this.getClass().getName());
        String code = this.getCode();
        if (code != null) {
            builder.append(" [").append(code).append("]");
        }
        if (message != null) {
            builder.append(": ").append(message);
        }
        return builder.toString();
    }

    public static BusinessException unsupportedOperation() {
        return new BusinessException("unsupported_operation", "条目当前状态下不能执行该操作");
    }

    public static BusinessException badRequest() {
        return new BusinessException("bad_request", "参数缺失");
    }

    public static BusinessException badRequest(String message) {
        return new BusinessException("bad_request", message);
    }

    public static BusinessException invalidStatus() {
        return new BusinessException("invalid_status", "条目状态异常");
    }


    public static BusinessException orderNotFound() {
        return new BusinessException("order_not_found", "未找到对应订单");
    }

    public static BusinessException shippingNotFound() {
        return new BusinessException("shipping_not_found", "未找到对应发货单");
    }

    public static BusinessException receiptNotFound() {
        return new BusinessException("receipt_not_found", "未找到对应签收单");
    }

    public static BusinessException returnNotFound() {
        return new BusinessException("return_not_found", "未找到对应退货单");
    }
}
